package com.eastfair.projectcore.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.projectcore.dto.*;
import com.eastfair.projectcore.entity.InterfaceInfo;
import com.eastfair.projectcore.entity.InterfaceMenu;
import com.eastfair.projectcore.service.InterfaceMenuService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.eastfair.annotation.annotation.security.PreAuth;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 接口与菜单关系
 * </p>
 *
 * @author ligang
 * @date 2023-05-11
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/interfaceMenu")
@Api(value = "InterfaceMenu", tags = "接口与菜单关系")
@PreAuth(replace = "projectcore:interfaceMenu:")
public class InterfaceMenuController extends SuperController<InterfaceMenuService, Long, InterfaceMenu, InterfaceMenuPageQuery, InterfaceMenuSaveDTO, InterfaceMenuUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<InterfaceMenu> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<InterfaceMenu> interfaceMenuList = list.stream().map((map) -> {
            InterfaceMenu interfaceMenu = InterfaceMenu.builder().build();
            //TODO 请在这里完成转换
            return interfaceMenu;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(interfaceMenuList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<InterfaceMenuPageQuery> params) {
        InterfaceMenuPageQuery pageQuery = params.getModel();

    }



    /**
     * 更新关联关系
     *
     * @param interfaceMenuDTO
     * @return
     */
    @ApiOperation(value = "更新关联关系", notes = "更新关联关系")
    @PostMapping("/addInterfaceOfMenu")
    @SysLog("更新关联关系")
    public R executeInterface(@RequestBody InterfaceMenuDTO interfaceMenuDTO) {
        InterfaceMenuDTO queryInterfaceMenuDTO = new InterfaceMenuDTO();
        queryInterfaceMenuDTO.setMenuId(interfaceMenuDTO.getMenuId());
        List<InterfaceMenu> interfaceMenuList = baseService.list(queryInterfaceMenuDTO, null);
        //先剔除之前的
        if (CollectionUtil.isNotEmpty(interfaceMenuList)) {
            for (InterfaceMenu interfaceMenu : interfaceMenuList) {
                interfaceMenu.setIsDeleted(BusinessConstant.YES);
                baseService.updateById(interfaceMenu);
            }
        }
        //重新添加
//        if (CollectionUtil.isNotEmpty(interfaceMenuDTO.getInterfaceUrls())) {
//            InterfaceInfoDTO interfaceInfoDTO = new InterfaceInfoDTO();
//            interfaceInfoDTO.setInterfaceIds(interfaceMenuDTO.getInterfaceIds());
//            R<List<InterfaceInfo>> listR = projectCoreApiFallback.querySystem(interfaceInfoDTO);
//            if (listR.getIsSuccess()) {
//                List<InterfaceInfo> interfaceInfoList = listR.getData();
//                for (Long id : interfaceMenuDTO.getInterfaceIds()) {
//                    InterfaceMenu interfaceMenu = new InterfaceMenu();
//                    interfaceMenu.setInterfaceId(id);
//                    interfaceMenu.setMenuId(interfaceMenuDTO.getMenuId());
//                    for (InterfaceInfo interfaceInfo : interfaceInfoList) {
//                        if (interfaceInfo.getId().equals(id)) {
//                            interfaceMenu.setFullUrl(interfaceInfo.getFullUrl());
//                        }
//                    }
//                    baseService.save(interfaceMenu);
//                }
//            }
//        }
        return R.success();
    }

}
